package com.iot.es.config;

import com.iot.es.properties.ESProperties;
import com.iot.es.service.ESSearchService;
import com.iot.es.service.IotEsTemplate;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;

/**
 * 描述: els快捷操作配置
 * @author: myx
 * @date: 2018/5/3
 * Copyright © 2018-hotpot. All rights reserved.
 */
@Configuration//开启配置
@EnableConfigurationProperties(ESProperties.class)//开启使用映射实体对象
@ConditionalOnClass(ESSearchService.class)//存在ESSearchService时初始化该配置类
@ConditionalOnProperty//存在对应配置信息时初始化该配置类
        (
                prefix = "iot-es",//存在配置前缀hello
                name = "isopen",
                havingValue = "true",//开启
                matchIfMissing = true//缺失检查
        )
public class ESConfiguration{
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ESProperties esProperties;
    /**
     * 根据条件判断不存在ElsService时初始化新bean到SpringIoc
     * @return
     */
    @Bean//创建HelloService实体bean
    @ConditionalOnMissingBean(ESSearchService.class)//缺失HelloService实体bean时，初始化HelloService并添加到SpringIoc
    public ESSearchService eSSearchService()
            throws Exception{
        log.info(">>>The ESSearchService Not Found，Execute Creat New Bean.");
        ESSearchService eSSearchService = new ESSearchService();
        // 配置信息
        Settings esSetting = Settings.builder().put("cluster.name", esProperties.getClusterName()).put("client.transport.sniff", true)// 增加嗅探机制，找到ES集群
                .put("thread_pool.search.size", Integer.parseInt(esProperties.getPoolSize()))// 增加线程池个数，暂时设为5
                .build();

        TransportClient client = new PreBuiltTransportClient(esSetting);
        InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(InetAddress.getByName(esProperties.getHostName()), Integer.valueOf(esProperties.getPort()));
        client.addTransportAddresses(inetSocketTransportAddress);
        eSSearchService.setClient(client);
        return eSSearchService;
    }


    /**
     * 根据条件判断不存在ElsService时初始化新bean到SpringIoc
     * @return
     */
    @Bean//创建HelloService实体bean
    @ConditionalOnMissingBean(IotEsTemplate.class)//缺失HelloService实体bean时，初始化HelloService并添加到SpringIoc
    public IotEsTemplate iotEsTemplate()
            throws Exception{
        log.info(">>>The ESSearchService Not Found，Execute Creat New Bean.");
        IotEsTemplate iotEsTemplate = new IotEsTemplate();
        // 配置信息
        Settings esSetting = Settings.builder().put("cluster.name", esProperties.getClusterName()).put("client.transport.sniff", true)// 增加嗅探机制，找到ES集群
                .put("thread_pool.search.size", Integer.parseInt(esProperties.getPoolSize()))// 增加线程池个数，暂时设为5
                .build();

        TransportClient client = new PreBuiltTransportClient(esSetting);
        InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(InetAddress.getByName(esProperties.getHostName()), Integer.valueOf(esProperties.getPort()));
        client.addTransportAddresses(inetSocketTransportAddress);
        iotEsTemplate.setClient(client);
        return iotEsTemplate;
    }

}